package com.linjiahao.serviceChat.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.linjiahao.serviceChat.config.MyChannelHandlerPool;
import com.linjiahao.serviceChat.service.FriendsAskService;
import com.linjiahao.serviceChat.vo.AddFriend;
import com.linjiahao.serviceChat.vo.AddMe_Info;
import com.linjiahao.serviceChat.vo.ChatMessage;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
@ChannelHandler.Sharable
@RabbitListener(queues = "topic.add-friend")
public class MyWebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {


    @Resource
    ChannelReadHandler channelReadHandler;

    @Resource
    FriendsAskService friendsAskService;

    public  ArrayList<String> message=new ArrayList<>();

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("与客户端建立连接，通道开启！");
        //添加到channelGroup通道组
        MyChannelHandlerPool.channelGroup.add(ctx.channel());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("与客户端断开连接，通道关闭！");
        //从channelGroup通道组删除
        MyChannelHandlerPool.channelGroup.remove(ctx.channel());


    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        System.out.println(".........");
        System.out.println(msg.text());
        System.out.println(".........");
        JSONObject jsonObject=JSONObject.parseObject(msg.text());
        if(jsonObject.getString("id")!=null){
            System.out.println("用户登陆");
            for (int i=0;i<this.message.size();i++) {

                if(this.message.get(i).equals(jsonObject.getString("id"))){
                    AddFriend addFriend = new AddFriend();
                    Object o = JSONObject.toJSON(addFriend);
                    String m=o.toString();
                    ctx.channel().writeAndFlush(new TextWebSocketFrame(m));
                    this.message.remove(i);
                }else if(this.message.get(i).substring(0,19).equals(jsonObject.getString("id"))){
                    ChatMessage chatMessage = new ChatMessage();
                    chatMessage.setMine("false");
                    chatMessage.setMyid(this.message.get(i).substring(20));
                    Object o1 = JSONObject.toJSON(chatMessage);
                    String m1=o1.toString();
                    ctx.channel().writeAndFlush(new TextWebSocketFrame(m1));
                    this.message.remove(i);
                }
            }
            MyChannelHandlerPool.channelIdMap.put(jsonObject.getString("id"),ctx.channel().id());
            List<AddMe_Info> addMeByList = friendsAskService.getAddMeByList(jsonObject.getString("id"));
            JSONArray jsonArray=new JSONArray(Collections.singletonList(addMeByList));
            ctx.channel().writeAndFlush(new TextWebSocketFrame(jsonArray.toString()));
            return;
        }
        //发送添加好友
        channelReadHandler.addFriend(msg.text());

        //发送信息
        channelReadHandler.sendMessage(msg.text());
    }



    @RabbitHandler
    public void process(String Rqmessage) {
        System.out.println("Receiver  : " + Rqmessage);
        message.add(Rqmessage);
    }

}

